// 切换登录/注册表单
function showRegister() {
    document.getElementById('loginForm').style.display = 'none';
    document.getElementById('registerForm').style.display = 'block';
}
function showLogin() {
    document.getElementById('registerForm').style.display = 'none';
    document.getElementById('loginForm').style.display = 'block';
}

// 注册功能
document.getElementById('registerForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('regUser').value;
    const city = document.getElementById('regCity').value;
    const password = document.getElementById('regPwd').value;

    // 密码校验
    if (password.length < 6) {
        alert('密码长度需≥6位');
        return;
    }

    // 存储用户数据到localStorage
    const user = { username, city, password };
    const users = JSON.parse(localStorage.getItem('users') || '[]');
    users.push(user);
    localStorage.setItem('users', JSON.stringify(users));
    alert('注册成功，请登录');
    showLogin();
});

// 登录功能
document.getElementById('loginForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const users = JSON.parse(localStorage.getItem('users') || '[]');
    const user = users.find(u => u.username === username && u.password === password);

    if (user) {
        // 存储当前登录用户
        localStorage.setItem('currentUser', JSON.stringify(user));
        window.location.href = 'home.html'; // 跳转至主页
    } else {
        document.getElementById('pwdError').textContent = '用户名或密码错误';
    }
});